//
//  TZLiveCell.m
//  wangYiLottery
//
//  Created by mac on 16/3/30.
//  Copyright © 2016年 tz. All rights reserved.
//

#import "TZLiveCell.h"
#import "TZLiveModel.h"


@interface TZLiveCell ()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *backView;
@end
@implementation TZLiveCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.titleLabel = [[UILabel alloc] init];
        self.backView = [[UIView alloc] init];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self setupUI];
}
//更新UI
- (void)setupUI {
    [self addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(5);
        make.top.equalTo(self.mas_top).offset(5);
        make.bottom.equalTo(self.mas_bottom).offset(-5);
    }];
    
    [self addSubview:self.backView];
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-10);
        make.top.equalTo(self.mas_top).offset(5);
        make.bottom.equalTo(self.mas_bottom).offset(-5);
        make.width.mas_equalTo(55);
    }];
}

- (void)setModel:(TZLiveModel *)model {
    _model = model;
    self.titleLabel.text = model.title;

    //获取key中对应的字符串
    NSString *accessoryView = model.accessoryView;
    //将key对应的字符串转化成一个类
    Class modelClass = NSClassFromString(accessoryView);
    //用类创建一个对象
    self.backView = [[modelClass alloc] init];
    if ([self.backView isKindOfClass:[UIButton class]]) {
        UIButton *timerButton = (UIButton *)self.backView;
        [timerButton setTitle:self.timerStr forState:UIControlStateNormal];
        [timerButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        timerButton.tintColor = [UIColor redColor];
    } else if ([self.backView isKindOfClass:[UISwitch class]]) {
        //监听开关
        UISwitch *sw = (UISwitch *)self.backView;
        [sw addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged];
        //初始化开关的状态
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        sw.on = [ud boolForKey:model.key];
    }
    //设置右边的View
    self.accessoryView = self.backView;
}

//开关状态的监听事件
- (void)switchChange:(UISwitch *)sender {
    //使用偏好设置保存开关状态
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setBool:sender.isOn forKey:self.model.key];
    //立即写入
    [ud synchronize];
}

+ (id)settingWithTableView:(UITableView *)tableView {
    static NSString *liveCellID = @"liveCellID";
    TZLiveCell *cell = [tableView dequeueReusableCellWithIdentifier:liveCellID];
    if (cell == nil) {
        cell = [[TZLiveCell alloc] init];
    }
    return cell;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
